from flask import Flask, jsonify
from flask import render_template
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import CheckConstraint

UKElec = Flask(__name__)
UKElec.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://DB_USER:DB_PASSWORD@localhost:3306/uk_2019election'

UK2019ElecDatabase = SQLAlchemy(UKElec)

from models import candidates, constituencies, parties, votes, results

from UK2019ElectionCalc import fetch_data, calculate_seats_based_on_constituencies, fptp_results, proportional_representation, proportional_representation_5threshold, proportional_representation_by_county, proportional_representation_by_region, proportional_representation_by_country, largest_remainder_by_county, largest_remainder_by_region, largest_remainder_by_country, dhondt_by_county, dhondt_by_region, dhondt_by_country

@UKElec.route("/")
def index():
    votes_data = votes.query.all()
    fptp_results_data = fptp_results(votes_data)
    spr = proportional_representation(votes_data)
    spr5 = proportional_representation_5threshold(votes_data)
    spr1 = proportional_representation_by_county(votes_data)
    print(spr1)
    return render_template("index.html", results = fptp_results_data, sprs = spr, sprs5 = spr5, sprs1 = spr1)
@UKElec.route("/SPR_Region")
def SPR_Region():
    votes_data = votes.query.all()
    spr2 = proportional_representation_by_region(votes_data)
    return render_template("SPR_Region.html", sprs2 = spr2)
@UKElec.route("/SPR_Country")
def SPR_Country():
    votes_data = votes.query.all()
    spr3 = proportional_representation_by_country(votes_data)
    return render_template("SPR_Country.html", sprs3 = spr3)
@UKElec.route("/largestRemainderByCounty")
def largestRemainderByCounty():
    lrbc_result = largest_remainder_by_county()
    return render_template("largestRemainderByCounty.html", lrbc_results = lrbc_result)
@UKElec.route("/largestRemainderByRegion")
def largestRemainderByRegion():
    lrbc_result1 = largest_remainder_by_region()
    return render_template("largestRemainderByRegion.html", lrbc_results1 = lrbc_result1)
@UKElec.route("/largestRemainderByCountry")
def largestRemainderByCountry():
    lrbc_result2 = largest_remainder_by_country()
    return render_template("largestRemainderByCountry.html", lrbc_results2 = lrbc_result2)
@UKElec.route("/dhondtByCounty")
def dhondtByCounty():
    votes_data = votes.query.all()
    dhondtByCounty = dhondt_by_county(votes_data)
    return render_template("dhondtByCounty.html", dhondtByCountys = dhondtByCounty)
@UKElec.route("/dhondtByRegion")
def dhondtByRegion():
    votes_data = votes.query.all()
    dhondtByRegion = dhondt_by_region(votes_data)
    return render_template("dhondtByRegion.html", dhondtByRegions = dhondtByRegion)
@UKElec.route("/dhondtByCountry")
def dhondtByCountry():
    votes_data = votes.query.all()
    dhondtByCountry = dhondt_by_country(votes_data)
    return render_template("dhondtByCountry.html", dhondtByCountrys = dhondtByCountry)
@UKElec.route("/data")
def data():
   votes_data = votes.query.all()
   seats_data = calculate_seats_based_on_constituencies(votes_data)
   print("Seat", seats_data)
   candidates_data = candidates.query.all()
   votes_data = votes.query.all()
   parties_data = parties.query.all()
   constituencies_data = constituencies.query.all()
   results_data = results.query.all()
   return render_template("data.html", seats=seats_data, candidates=candidates_data, votes=votes_data, parties=parties_data, constituencies=constituencies_data, results=results_data)

if __name__ == "__main__":
    UKElec.run(debug = True)